﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class ConfigApp : IConfig
    {
        private string dbname = "Systems";

        public void Add(ConfigEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Config(");
            builder.Append("IsUseProc,IsUseSale,IsUseStock,IsUseMeUse,IsUseTeaDunk,IsUseAgio,IsUseDaoHang,IsUseHelp");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.IsUseProc + ",");
            builder.Append("" + model.IsUseSale + ",");
            builder.Append("" + model.IsUseStock + ",");
            builder.Append("" + model.IsUseMeUse + ",");
            builder.Append("" + model.IsUseTeaDunk + ",");
            builder.Append("" + model.IsUseAgio + ",");
            builder.Append("" + model.IsUseDaoHang + ",");
            builder.Append("" + model.IsUseHelp + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int ConfigId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete T_Config ");
            builder.Append(" where ConfigId=" + ConfigId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Config ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by ConfigId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "ConfigId", "T_Config", swhere);
        }

        public ConfigEnt GetModel(int ConfigId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ConfigId,IsUseProc,IsUseSale,IsUseStock,IsUseMeUse,IsUseTeaDunk,IsUseAgio,IsUseDaoHang,IsUseHelp from T_Config ");
            builder.Append(" where ConfigId=" + ConfigId);
            ConfigEnt ent = new ConfigEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.ConfigId = ConfigId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["IsUseProc"].ToString() != "")
                {
                    ent.IsUseProc = int.Parse(table.Rows[0]["IsUseProc"].ToString());
                }
                if (table.Rows[0]["IsUseSale"].ToString() != "")
                {
                    ent.IsUseSale = int.Parse(table.Rows[0]["IsUseSale"].ToString());
                }
                if (table.Rows[0]["IsUseStock"].ToString() != "")
                {
                    ent.IsUseStock = int.Parse(table.Rows[0]["IsUseStock"].ToString());
                }
                if (table.Rows[0]["IsUseMeUse"].ToString() != "")
                {
                    ent.IsUseMeUse = int.Parse(table.Rows[0]["IsUseMeUse"].ToString());
                }
                if (table.Rows[0]["IsUseTeaDunk"].ToString() != "")
                {
                    ent.IsUseTeaDunk = int.Parse(table.Rows[0]["IsUseTeaDunk"].ToString());
                }
                if (table.Rows[0]["IsUseAgio"].ToString() != "")
                {
                    ent.IsUseAgio = int.Parse(table.Rows[0]["IsUseAgio"].ToString());
                }
                if (table.Rows[0]["IsUseDaoHang"].ToString() != "")
                {
                    ent.IsUseDaoHang = int.Parse(table.Rows[0]["IsUseDaoHang"].ToString());
                }
                if (table.Rows[0]["IsUseHelp"].ToString() != "")
                {
                    ent.IsUseHelp = int.Parse(table.Rows[0]["IsUseHelp"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Config ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "ConfigId");
        }

        public void Update(ConfigEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Config set ");
            builder.Append("IsUseProc=" + model.IsUseProc + ",");
            builder.Append("IsUseSale=" + model.IsUseSale + ",");
            builder.Append("IsUseStock=" + model.IsUseStock + ",");
            builder.Append("IsUseMeUse=" + model.IsUseMeUse + ",");
            builder.Append("IsUseTeaDunk=" + model.IsUseTeaDunk + ",");
            builder.Append("IsUseAgio=" + model.IsUseAgio + ",");
            builder.Append("IsUseDaoHang=" + model.IsUseDaoHang + ",");
            builder.Append("IsUseHelp=" + model.IsUseHelp + "");
            builder.Append(" where ConfigId=" + model.ConfigId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

